# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=pyfilesystem2
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"  "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.4.11
pkgrel=1
pkgdesc="Python's filesystem abstraction layer (mingw-w64)"
arch=('any')
url='https://github.com/PyFilesystem/pyfilesystem2'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3-appdirs"
             "${MINGW_PACKAGE_PREFIX}-python2-enum34"
             "${MINGW_PACKAGE_PREFIX}-python2-backports.os"
             "${MINGW_PACKAGE_PREFIX}-python3-pytz"
             "${MINGW_PACKAGE_PREFIX}-python2-scandir"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-six"
             "${MINGW_PACKAGE_PREFIX}-python2-typing"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-appdirs"
             "${MINGW_PACKAGE_PREFIX}-python3-pytz"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-six")
# We would require "${MINGW_PACKAGE_PREFIX}-python2-pyftpdlib" amd "${MINGW_PACKAGE_PREFIX}-python3-pyftpdlib"
#checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-appdirs" 
#              "${MINGW_PACKAGE_PREFIX}-python2-mock"
#              "${MINGW_PACKAGE_PREFIX}-python2-pytz"
#              "${MINGW_PACKAGE_PREFIX}-python2-pyftpdlib"
#              "${MINGW_PACKAGE_PREFIX}-python3-appdirs" 
#              "${MINGW_PACKAGE_PREFIX}-python3-mock"
#              "${MINGW_PACKAGE_PREFIX}-python3-pytz"
#              "${MINGW_PACKAGE_PREFIX}-python3-pyftpdlib")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/PyFilesystem/pyfilesystem2/archive/v${pkgver}.tar.gz")
sha256sums=('246424187f5a509b6dc1573c66fb203e375eab79f540dbd829dd058082be98b1')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
}

build() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

# We would require "${MINGW_PACKAGE_PREFIX}-python2-pyftpdlib" amd "${MINGW_PACKAGE_PREFIX}-python3-pyftpdlib"
#check() {
#  for pver in {2,3}; do
#    msg "Python ${pver} test for ${CARCH}"
#    cd "${srcdir}/python${pver}-build-${CARCH}"
#    ${MINGW_PREFIX}/bin/python${pver} setup.py test
#  done
#}

package_python3-pyfilesystem2() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-appdirs"
           "${MINGW_PACKAGE_PREFIX}-python3-pytz"
           "${MINGW_PACKAGE_PREFIX}-python3-six")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}

package_python2-pyfilesystem2() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-appdirs"
           "${MINGW_PACKAGE_PREFIX}-python2-backports.os"
           "${MINGW_PACKAGE_PREFIX}-python2-pytz"
           "${MINGW_PACKAGE_PREFIX}-python2-enum34"
           "${MINGW_PACKAGE_PREFIX}-python2-scandir"
           "${MINGW_PACKAGE_PREFIX}-python2-six"
           "${MINGW_PACKAGE_PREFIX}-python2-typing")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"
}

package_mingw-w64-i686-python2-pyfilesystem2() {
  package_python2-pyfilesystem2
}

package_mingw-w64-i686-python3-pyfilesystem2() {
  package_python3-pyfilesystem2
}

package_mingw-w64-x86_64-python2-pyfilesystem2() {
  package_python2-pyfilesystem2
}

package_mingw-w64-x86_64-python3-pyfilesystem2() {
  package_python3-pyfilesystem2
}
